package com.qing.design.patterns.iterator.example.profile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//社交档案
public class Profile {

	private String name;

	private String email;
	//联络
	private Map<String, List<String>> contacts=new HashMap<>();


	public Profile(String email,String name, String... contacts) {
		this.name = name;
		this.email = email;
		// Parse contact list from a set of "friend:email@gmail.com" pairs.
		for (String contact : contacts) {
			String[] parts=contact.split(":");
			String contactType="friend";
			String contactEmail;
			if(parts.length==1){
				contactEmail=parts[0];
			}else{
				contactType=parts[0];
				contactEmail=parts[1];
			}
			//TODO 判空操作十分高效，附初始值，避免空指针异常
			if(!this.contacts.containsKey(contactType)){
				this.contacts.put(contactType,new ArrayList<>());
			}
			this.contacts.get(contactType).add(contactEmail);
		}
	}

	public String getName() {
		return name;
	}


	public String getEmail() {
		return email;
	}


	public List<String> getContacts(String contactType) {
		//TODO 判空操作十分高效，附初始值，避免空指针异常
		if(!this.contacts.containsKey(contactType)){
			this.contacts.put(contactType,new ArrayList<>());
		}
		return contacts.get(contactType);
	}


}
